/**
 *=============================================================================
 *  @copyright (C)      2025CCUT_1024lab_Damocles
 *
 *  @file    template.c
 *  @brief   封装模块代码规范
 *
 *  @note    使用方法
 *
 *  @attention 注意事项
 *-----------------------------------------------------------------------------
 *  @version    @author    @date    @note
 *  V1.0.0      Cherno    250808    done
 *
 *==============================================================================
 */

/*==============================Includes======================================*/
#include "drv_uart.h"
/*==============================PrivateMacros=================================*/

/*==============================PrivateTypes==================================*/

/*==============================PrivateVariables==============================*/
uart_manager_t uart1_manager = {0};
uart_manager_t uart2_manager = {0};
uart_manager_t uart3_manager = {0};
uart_manager_t uart4_manager = {0};
uart_manager_t uart5_manager = {0};
uart_manager_t uart6_manager = {0};
/*==============================PrivateFunctionDeclarations===================*/

/*==============================FunctionPrototypes============================*/

/**
 * @brief : 描述该函数主要功能
 * @note : 注意事项
 *
 * @param : 参数名（与函数里的参数一致）  参数的描述
 * @return : 返回值的说明
 */
void uart_init(UART_HandleTypeDef *huart, uart_call_back call_back_function,
               uint16_t rx_buffer_size)
{
	if (huart->Instance == USART1) {
		uart1_manager.huart = huart;
		uart1_manager.call_back_function = call_back_function;
		uart1_manager.rx_buffer_size = rx_buffer_size;
		HAL_UARTEx_ReceiveToIdle_DMA(huart, uart1_manager.rx_buffer,
		                             uart1_manager.rx_buffer_size);
	} else if (huart->Instance == USART2) {
		uart2_manager.huart = huart;
		uart2_manager.call_back_function = call_back_function;
		uart2_manager.rx_buffer_size = rx_buffer_size;
		HAL_UARTEx_ReceiveToIdle_DMA(huart, uart2_manager.rx_buffer,
		                             uart2_manager.rx_buffer_size);
	} else if (huart->Instance == USART3) {
		uart3_manager.huart = huart;
		uart3_manager.call_back_function = call_back_function;
		uart3_manager.rx_buffer_size = rx_buffer_size;
		HAL_UARTEx_ReceiveToIdle_DMA(huart, uart3_manager.rx_buffer,
		                             uart3_manager.rx_buffer_size);
	} else if (huart->Instance == UART4) {
		uart4_manager.huart = huart;
		uart4_manager.call_back_function = call_back_function;
		uart4_manager.rx_buffer_size = rx_buffer_size;
		HAL_UARTEx_ReceiveToIdle_DMA(huart, uart4_manager.rx_buffer,
		                             uart4_manager.rx_buffer_size);
	} else if (huart->Instance == UART5) {
		uart5_manager.huart = huart;
		uart5_manager.call_back_function = call_back_function;
		uart5_manager.rx_buffer_size = rx_buffer_size;
		HAL_UARTEx_ReceiveToIdle_DMA(huart, uart5_manager.rx_buffer,
		                             uart5_manager.rx_buffer_size);
	} else if (huart->Instance == USART6) {
		uart6_manager.huart = huart;
		uart6_manager.call_back_function = call_back_function;
		uart6_manager.rx_buffer_size = rx_buffer_size;
		HAL_UARTEx_ReceiveToIdle_DMA(huart, uart6_manager.rx_buffer,
		                             uart6_manager.rx_buffer_size);
	}
}

uint8_t uart_send_data(UART_HandleTypeDef *huart, uint8_t *data,
                       uint16_t length)
{
	return (HAL_UART_Transmit(huart, data, length, 200));
}

uint8_t uart_send_data_dma(UART_HandleTypeDef *huart, uint8_t *data,
                           uint16_t length)
{
	return (HAL_UART_Transmit_DMA(huart, data, length));
}

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef *huart, uint16_t size)
{
	// //判断程序初始化完成
	// if(init_finished == 0)
	// {
	//     return;
	// }

	// 选择回调函数
	if (huart->Instance == USART1) {
		uart1_manager.call_back_function(uart1_manager.rx_buffer, size);
		HAL_UARTEx_ReceiveToIdle_DMA(huart, uart1_manager.rx_buffer,
		                             uart1_manager.rx_buffer_size);
	} else if (huart->Instance == USART2) {
		uart2_manager.call_back_function(uart2_manager.rx_buffer, size);
		HAL_UARTEx_ReceiveToIdle_DMA(huart, uart2_manager.rx_buffer,
		                             uart2_manager.rx_buffer_size);
	} else if (huart->Instance == USART3) {
		uart3_manager.call_back_function(uart3_manager.rx_buffer, size);
		HAL_UARTEx_ReceiveToIdle_DMA(huart, uart3_manager.rx_buffer,
		                             uart3_manager.rx_buffer_size);
	} else if (huart->Instance == UART4) {
		uart4_manager.call_back_function(uart4_manager.rx_buffer, size);
		HAL_UARTEx_ReceiveToIdle_DMA(huart, uart4_manager.rx_buffer,
		                             uart4_manager.rx_buffer_size);
	} else if (huart->Instance == UART5) {
		uart5_manager.call_back_function(uart5_manager.rx_buffer, size);
		HAL_UARTEx_ReceiveToIdle_DMA(huart, uart5_manager.rx_buffer,
		                             uart5_manager.rx_buffer_size);
	} else if (huart->Instance == USART6) {
		uart6_manager.call_back_function(uart6_manager.rx_buffer, size);
		HAL_UARTEx_ReceiveToIdle_DMA(huart, uart6_manager.rx_buffer,
		                             uart6_manager.rx_buffer_size);
	}
}

/*========================COPYRIGHT(C) CCUT-DAMOCLES==========================*/
